#include <QPushButton>
#include "name-tab-dialog.h"
#include "ui_name-tab-dialog.h"

NameTabDialog::NameTabDialog(QWidget *parent) :
    QDialog(parent), ui(new Ui::NameTabDialog)
{
    ui->setupUi(this);
    setWindowFlags((windowFlags() & ~Qt::WindowContextHelpButtonHint));
    connect (ui->nameLineEdit, &QLineEdit::textChanged, this, &NameTabDialog::validateName);
    validateName();
}

NameTabDialog::~NameTabDialog()
{
    delete ui;
}

void NameTabDialog::setDialogMode (DialogMode dialogMode)
{
    switch (dialogMode)
    {
    case AddTabMode:
        setWindowTitle("Dodaj kartę");
        break;
    case RenameTabMode:
        setWindowTitle("Zmień nazwę karty");
        break;
    default:
        break;
    }
}

void NameTabDialog::setExistingNames (const QList<QString> &existingNames)
{
    m_existingNames = existingNames;
}

void NameTabDialog::validateName()
{
    QString name = ui->nameLineEdit->text();
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!name.isEmpty() && !m_existingNames.contains(name));
}

QString NameTabDialog::name() const
{
    return ui->nameLineEdit->text();
}
